package ru.rchervot.sanasar.client.view;

import ru.rchervot.sanasar.client.event.EventPageChange;
import ru.rchervot.sanasar.client.event.EventQueryChange;
import ru.rchervot.sanasar.client.event.HandlerPageChange;
import ru.rchervot.sanasar.client.model.FactoryQuery;
import ru.rchervot.sanasar.client.model.Query;
import ru.rchervot.sanasar.client.model.ResultsPage;
import ru.rchervot.sanasar.client.model.SearchResultsItem;
import ru.rchervot.sanasar.client.rpc.GetPage;
import ru.rchervot.sanasar.client.rpc.GetQuery;
import ru.rchervot.sanasar.client.rpc.GotPage;
import ru.rchervot.sanasar.client.rpc.GotQuery;
import ru.rchervot.sanasar.client.rpc.RPCServiceAsync;
import ru.rchervot.sanasar.client.rpc.SaveQuery;
import ru.rchervot.sanasar.client.rpc.SavedQuery;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerManager;

public abstract class AbstractSearchBoxPresenter implements SearchBoxPresenter {
	
	private final Display display;
	protected final HandlerManager eventBus;
	protected final RPCServiceAsync service;
	protected final FactoryQuery factoryQuery;
	protected abstract GwtEvent<?> getFiltersShowEvent();
	protected abstract void addHandlerFiltersVisibility();
	
	private static final int TEMP_USER_ID = 133;
	private Integer queryIdFilters; 
	
	public AbstractSearchBoxPresenter(Display display, HandlerManager eventBus,
			RPCServiceAsync service, FactoryQuery factoryQuery) {
		this.display = display;
		this.eventBus = eventBus;
		this.service = service;
		this.factoryQuery = factoryQuery;
		
		addHandlerFiltersVisibility();
		listenForPageChange();
	}
	
	@Override
	public void bindDisplay() {
		display.setPresenter(this);
		
		display.getSearchButton().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				String queryString = display.getSearchField().getValue();
				if (queryString != null && queryString.length() > 0) {
					Query query = factoryQuery.buildFromQuickSearch(queryString, TEMP_USER_ID);
					
					service.execute(new SaveQuery(query),
		    			new SavedQuery() {
		    		@Override
		    		public void saved(Integer queryId) {
		    			service.execute(new GetPage(queryId, 0),
				    			new GotPage<SearchResultsItem>() {
				    		public void got(ResultsPage<SearchResultsItem> page) {
				    		}
				    	});
		    		}
		    	});
				}
			}
		});
		
		display.getFiltersButton().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (queryIdFilters != null) {
					service.execute(new GetQuery(queryIdFilters),
		    			new GotQuery() {
		    		public void got(Query query) {
		    		}
		    	});
				} else {
					eventBus.fireEvent(EventQueryChange.build(
							factoryQuery.buildEmptyQuery(TEMP_USER_ID)));
				}
						
				eventBus.fireEvent(getFiltersShowEvent());
			}
		});
	}
	
	protected void setVisibleFilters(boolean visible) {
		display.setVisibleFilters(visible);
	}
	
	protected void listenForPageChange() {
		eventBus.addHandler(EventPageChange.getType(),
				new HandlerPageChange<SearchResultsItem>() {
			@Override
			public void onPageChange(
					EventPageChange<SearchResultsItem> event) {
				if (event.isStartPage()) {
					queryIdFilters = null;
				} else {
					queryIdFilters = event.getPage().getQueryID();
				}
			}
		});
	}
	
}
